﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Media;

namespace LunarSF.SHomeWorkshop.LunarMarkdownEditor
{
    public class FindTaskListItem : FindLineTreeViewItem
    {
        public FindTaskListItem(string fileFullPath, string fileShortName, int lineNumber, int startLineOffset, int keywordLength, string lineText, Brush foreColor, ItemType type, TextDecorationCollection textDecorations = null) : base(fileFullPath, fileShortName, lineNumber, startLineOffset, keywordLength, lineText, foreColor, type, textDecorations)
        {
            this.MouseDown += FindTaskListItem_MouseDown;
        }

        private void FindTaskListItem_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            e.Handled = true;
            this.IsSelected = true;
        }

        /// <summary>
        /// 用于刷新StartDateTime和EndDateTime的值。
        /// </summary>
        public void UpdateDateTime()
        {
            DateTime? dtS = null;
            DateTime? dtE = null;

            foreach (var subitem in this.Items)
            {
                var item = subitem as FindTaskListTimeTagItem;
                if (item == null) continue;

                if (dtS == null || dtS.HasValue == false) dtS = item.TagDateTime;
                else
                {
                    if (dtS.Value.CompareTo(item.TagDateTime.Value) > 0)
                    {
                        dtS = item.TagDateTime;
                    }
                }

                if (dtE == null || dtE.HasValue == false) dtE = item.TagDateTime;
                else
                {
                    if (dtE.Value.CompareTo(item.TagDateTime.Value) < 0)
                    {
                        dtE = item.TagDateTime;
                    }
                }
            }

            if (dtS != null && dtS.HasValue) StartDateTime = dtS.Value;

            if (dtE != null && dtE.HasValue) EndDateTime = dtE.Value;
        }

        /// <summary>
        /// Begin time Or First time.
        /// 注意：在使用之前应调用UpdateDateTime()方法刷新其值。
        /// </summary>
        public DateTime StartDateTime { get; private set; }

        /// <summary>
        /// Finish time Or Abort time.
        /// 注意：在使用之前应调用UpdateDateTime()方法刷新其值。
        /// </summary>
        public DateTime EndDateTime { get; private set; }
    }
}
